// pages/order/order.js
//获取应用实例
const app = getApp();
const api = require('../../utils/api.js');

Page({
  /**
   * 页面的初始数据
   * 每次访问订单界面 后台都会重新写入更新开始时间和结束时间
      前端页面先获取后台开始时间结束时间
   */
  data: {
    hours: 0,
    minuters: 0,
    seconds: 0,
    umbrellaId: null,
    startTime: null,
    orderId: null,
    rentId: null,
    endTime: ''
  },
  // 获取当前用户的订单信息
  getOrderData() {
    const that = this;
    wx.showLoading({
      title: '正在获取数据',
    });
    api.proceedOrder(app.globalData.token).then(res => {
      wx.hideLoading();
      const orderData = res.data.data;
      that.setData({
        umbrellaId: orderData.umbrellaId,
        orderId: orderData.id,
      });
    });
  },
  // 根据输入框更新值
  bindKeyInput: function (e) {
    this.setData({
      rentId: e.detail.value
    });
  },
  // 点击了结束用伞按钮
  handleEnd() {
    app.globalData.close=false;
    wx.setStorage({
      key: 'close',
      data: {
        flage: false
      },
      success: function (res) {
       
   
      }
    })
    const that = this;
    clearInterval(that.timer);
    wx.showLoading({
      title: '正在获取数据',
    });
    api.finishOrder(app.globalData.token, that.data.orderId, that.data.rentId)
      .then(res => {
        wx.hideLoading();
        wx.redirectTo({
          url: '../pay/pay'
        });
      })
      .catch(err => {
        wx.hideLoading();
        wx.showModal({
          title: '操作失败',
          content: '网络跑到外星球了',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              // console.log('用户点击确定');
            }
          }
        });
      })
      
  },

  //时间相减
  timerr() { 
    const that = this;
    api.proceedOrders(app.globalData.token).then(res => {
      console.log(res)
      const orderData = res.data.data;
      that.setData({ 
        startTime: orderData.startTime,
        endTime: orderData.endTime
      });
      var sdate = new Date(that.data.startTime);
      var now = new Date(that.data.endTime);
      var days = now.getTime() - sdate.getTime();
      var leave1 = days % (24 * 3600 * 1000) 
      //计算天数后剩余的毫秒数  
      var hours = Math.floor(leave1 / (3600 * 1000))
      //计算相差分钟数  
      var leave2 = leave1 % (3600 * 1000) 
      //计算小时数后剩余的毫秒数  
      var minutes = Math.floor(leave2 / (60 * 1000))
      //计算相差秒数  
      var leave3 = leave2 % (60 * 1000) 
      //计算分钟数后剩余的毫秒数  
      var seconds = Math.round(leave3 / 1000)
      console.log(" 相差 " + hours + "小时 " + minutes + " 分钟" + seconds + " 秒");
      // 初始化计时器
      this.setData({
        hours: hours,
        minuters: minutes,
        seconds: seconds,
      });
    // 计时开始
    this.timer = setInterval(() => {
      this.setData({
        seconds: seconds++
      })
      if (seconds == 60) {
        seconds = 0;
        minutes++;
        setTimeout(() => {
          this.setData({
            minuters: minutes
          });
        }, 1000)
        if (minutes == 60) {
          minutes = 0;
          hours++
          setTimeout(() => {
            this.setData({
              hours: hours
            });
          }, 1000)
        }
      };
    }, 1000)
  
    });

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    this.timerr();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getOrderData();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  Hide: function () {


  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {


  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})